package org.douxiaoban.client.adapter;

import java.util.ArrayList;
import java.util.List;

import org.douxiaoban.client.api.DoubanList;
import org.douxiaoban.data.DoubanBaseData;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * 抽象类 DoubanAdapter 继承它的有 MiniBlogAdapter， MailAdapter, userAdapter
* Description: （ 
* Copyright:   Copyright (c)2011  
* @author:     6a209  
* Create at:   2011-7-16 上午01:11:03
 */
public abstract class DoubanAdapter extends BaseAdapter{
	
	protected Context mContext;
	protected List<DoubanBaseData> mListData;
	
	protected DoubanList mDoubanList;
	
	public DoubanAdapter(Context context, List<? extends DoubanBaseData> list){
		
		mContext = context;
		mListData = (List<DoubanBaseData>)list;
	}
	
	@Override
	public int getCount(){
		return mListData.size(); 
	}
	
	@Override
	public Object getItem(int index){
		if(index < 0 || index >= mListData.size()){
			return null;
		}
		return mListData.get(index);
	}
	
	@Override
	public long getItemId(int index){
		return 0;
	} 
	
	
	//
	// 每个子类必须从新实现 getView方法
	public abstract View getView(int position, View view, ViewGroup vGroup);
	
	/**
	 * 获取列表第一项的id值，
	 * 刷新操作时要根据此id做插入操作
	* @param  
	* @return 
	* @exception
	 */
	public int getFirstId(){
		if(!mListData.isEmpty()){
			DoubanBaseData data = mListData.get(0);
			return data.mId;	
		}
		return -1;
	}
	
	public void setList(DoubanList list){
		mDoubanList = list;
	}
	
	/**
	 * 获取列表的最后一项的id值，获取更多时
	 * 要根据此id做插入操作
	* @param  
	* @return 
	* @exception
	 */
	public int getLastId(){
		if(!mListData.isEmpty()){
			DoubanBaseData data = mListData.get(mListData.size()-1);
			return data.mId;
		}
		return -1;
	}
	
	/**
	 * 更多操作往后插入数据
	* @param  待插入的数据
	* @return 
	* @exception
	 */
	public void addDataTail(ArrayList<? extends DoubanBaseData> list){
		mListData.addAll(list);
		this.notifyDataSetChanged();
	}
	
	/**
	 * 刷新操作，往前插入数据
	* @param  待插入的数据
	* @return 
	* @exception
	 */
	public void addDataPre(ArrayList<? extends DoubanBaseData> list){
		mListData.addAll(0,  list);
		this.notifyDataSetChanged();
	}
	
	/**
	 * 删除操作
	* @param  将要被删除的位置
	* @return 
	* @exception
	 */
	public boolean remove(int index){
		if(0 > index  || index > mListData.size()){
			return false;
		}
		mListData.remove(index);
		this.notifyDataSetChanged();
		return true;
	}

}